package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.TestExamPaper;
import com.ruoyi.domain.vo.TestExamPaperVo;
import com.ruoyi.service.ITestExamPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考试平台卷库Controller
 */
@Api(tags = "卷库管理接口")
@RestController
@RequestMapping("/exam/paper")
public class TestExamPaperController extends BaseController
{
    @Autowired
    private ITestExamPaperService testExamPaperService;

    /**
     * 查询考试平台卷库列表
     */
    @ApiOperation("查询考试平台卷库列表")
    @GetMapping("/list")
    public TableDataInfo list(String subjectName,String examName,String grade)
    {
        startPage();
        List<TestExamPaperVo> list = testExamPaperService.selectTestExamPaperList(subjectName,examName,grade);
        return getDataTable(list);
    }


    /**
     * 获取考试平台卷库详细信息
     */
    @ApiOperation("获取考试平台卷库详细信息")
    @GetMapping(value = "/{examId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId)
    {
        if(testExamPaperService.selectTestExamPaperByExamId(examId)!=null){
            return success(testExamPaperService.selectTestExamPaperByExamId(examId));
        }
        else {
            return AjaxResult.error("查无此试卷");
        }
    }

    /**
     * 新增考试平台卷库
     */
    @ApiOperation("新增考试平台卷库")
    @PostMapping
    public AjaxResult add(@RequestBody TestExamPaper testExamPaper)
    {
        if(!testExamPaperService.selectTestExamPaperList(testExamPaper.getSubjectName(),
                testExamPaper.getExamName(),testExamPaper.getGrade()).isEmpty()){
            return AjaxResult.error("添加数据已重复");
    }else {
            return toAjax(testExamPaperService.insertTestExamPaper(testExamPaper));
        }
    }

    /**
     * 修改考试平台卷库
     */
    @ApiOperation("修改考试平台卷库")
    @PutMapping
    public AjaxResult edit(@RequestBody TestExamPaper testExamPaper)
    {
        return toAjax(testExamPaperService.updateTestExamPaper(testExamPaper));
    }

    /**
     * 删除考试平台卷库
     */
    @ApiOperation("删除考试平台卷库")
    @DeleteMapping
    public AjaxResult remove(@RequestBody TestExamPaper testExamPaper)
    {
        return toAjax(testExamPaperService.updateTestExamPaper(testExamPaper));
    }
}
